<template>
  <el-main class="dashboard-main">
    <div class="dashboard-container">
      <!-- 页面标题 -->
      <div class="page-header mb-8">
        <div class="header-top">
          <div>
            <h2 class="page-title">简历管理中心</h2>
            <p class="page-desc">管理你的所有简历、收藏和投递记录</p>
          </div>
          
        </div>
      </div>

      <!-- 快捷操作区 -->
      <el-row :gutter="24" class="quick-actions mb-8">
        <el-col :span="6">
          <el-card class="action-card" @click="$router.push('/editor')">
            <div class="action-content">
              <div class="action-icon">+</div>
              <div class="action-text">
                <h3 class="action-title">创建新简历</h3>
                <p class="action-desc">从模板开始制作</p>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="action-card" @click="$router.push('/template/create')">
            <div class="action-content">
              <div class="action-icon">🎨</div>
              <div class="action-text">
                <h3 class="action-title">创建模板</h3>
                <p class="action-desc">自定义简历样式</p>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="action-card" @click="$router.push('/my-resumes')">
            <div class="action-content">
              <div class="action-icon">↑</div>
              <div class="action-text">
                <h3 class="action-title">上传PDF简历</h3>
                <p class="action-desc">导入现有简历</p>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="action-card" @click="$router.push('/resume-share')">
            <div class="action-content">
              <div class="action-icon">📤</div>
              <div class="action-text">
                <h3 class="action-title">简历分享</h3>
                <p class="action-desc">发现优秀模板</p>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <!-- 第二行快捷操作 -->
      <el-row :gutter="24" class="quick-actions mb-8">
        <el-col :span="6">
          <el-card class="action-card" @click="$router.push('/resume-favorites')">
            <div class="action-content">
              <div class="action-icon">⭐</div>
              <div class="action-text">
                <h3 class="action-title">我的收藏</h3>
                <p class="action-desc">查看收藏的简历</p>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 我的简历列表（只显示最新3条） -->
      <div class="resume-list mb-8">
        <div class="list-header mb-4">
          <h3 class="list-title">最近简历</h3>
          <el-button text class="view-all-btn" @click="$router.push('/my-resumes')">
            查看更多 →
          </el-button>
        </div>

        <el-row :gutter="24" v-if="resumeList.length > 0">
          <el-col :span="8" v-for="resume in resumeList.slice(0, 3)" :key="resume.id">
            <ResumeCard 
              :resumeId="resume.id"
              :title="resume.resume_name" 
              :date="formatDate(resume.updated_at)" 
              :isDefault="false"
              @refresh="loadResumeList"
            />
          </el-col>
        </el-row>

        <el-empty 
          v-else 
          description="还没有创建简历，点击上方【创建新简历】开始吧"
          :image-size="120"
        />
      </div>

    </div>
  </el-main>

  <!-- 页脚 -->
  <el-footer class="dashboard-footer">
    <div class="footer-container">
      <div class="footer-logo">
        <span class="logo-text">ResumeHub</span>
      </div>
      <p class="copyright">© 2023 ResumeHub. 保留所有权利</p>
    </div>
  </el-footer>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import instance from '@/utils/request'
import ResumeCard from '../../components/ResumeCard.vue'

// 返回首页按钮及逻辑已移除

const resumeList = ref([])

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return '-'
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 从后端获取简历列表
const loadResumeList = async () => {
  try {
    console.log('📥 加载简历列表...')
    const response = await instance.request({
      method: 'GET',
      url: '/resumes'
    })
    
    if (response.code === 0) {
      resumeList.value = response.data.list || []
      console.log('✅ 简历列表加载成功，数量:', resumeList.value.length)
    } else {
      ElMessage.error(response.msg || '加载简历列表失败')
    }
  } catch (error) {
    console.error('❌ 加载简历列表失败:', error)
    ElMessage.error('加载简历列表失败，请检查网络连接')
  }
}

// 页面加载时获取简历列表
onMounted(() => {
  loadResumeList()
})
</script>

<style scoped>
/* 主页整体样式 */
.dashboard-main {
  padding: 24px !important;
  background-color: #fafafa !important;
}

.dashboard-container {
  max-width: 1200px;
  margin: 0 auto;
}

/* 页面标题 */
.page-header {
  margin-bottom: 32px;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.page-desc {
  font-size: 14px;
  color: #999;
}

/* 快捷操作卡片 */
.quick-actions {
  margin-bottom: 32px;
}

.action-card {
  border-radius: 10px !important;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.06) !important;
  border: none !important;
  transition: all 0.3s ease !important;
  cursor: pointer;
  overflow: hidden !important;
}

.action-card:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1) !important;
  transform: translateY(-2px) !important;
}

.action-content {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
}

.action-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #e8f3ff;
  color: #165dff;
  font-size: 20px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-text .action-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 4px;
}

.action-text .action-desc {
  font-size: 13px;
  color: #999;
}

/* 简历列表 */
.resume-list {
  margin-bottom: 32px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.list-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.view-all-btn {
  color: #165dff !important;
  font-size: 13px !important;
  font-weight: 500 !important;
}

/* 统计卡片 */
.stats-card {
  border-radius: 10px !important;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.06) !important;
  border: none !important;
  overflow: hidden !important;
}

.stats-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px !important;
  border-bottom: 1px solid #f5f5f5 !important;
}

.stats-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.time-radio {
  display: flex;
  gap: 8px;
}

.radio-btn {
  border-radius: 6px !important;
  padding: 4px 12px !important;
  font-size: 13px !important;
}

::v-deep .el-radio-button__inner {
  border-radius: 6px !important;
}

::v-deep .el-radio-button.is-active .el-radio-button__inner {
  background-color: #165dff !important;
  border-color: #165dff !important;
}

/* 图表容器 */
.chart-container {
  height: 260px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
  margin: 20px;
  border-radius: 8px;
}

.empty-chart {
  --el-empty-description-color: #999 !important;
  --el-empty-image-size: 80px !important;
}

/* 统计数字 */
.stats-row {
  padding: 0 20px 20px;
}

.stat-item {
  text-align: center;
  padding: 12px;
  background-color: #fafafa;
  border-radius: 8px;
}

::v-deep .el-statistic__label {
  font-size: 13px !important;
  color: #999 !important;
  margin-bottom: 8px !important;
}

::v-deep .el-statistic__content {
  font-size: 20px !important;
  font-weight: 600 !important;
  color: #333 !important;
}

.stat-change {
  font-size: 12px !important;
  color: #67c23a !important;
  margin-left: 4px;
}

/* 页脚 */
.dashboard-footer {
  background-color: #fff !important;
  border-top: 1px solid #f0f0f0 !important;
  padding: 20px 0 !important;
}

.footer-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.footer-logo .logo-text {
  font-size: 16px;
  font-weight: 600;
  color: #165dff;
}

.copyright {
  font-size: 12px;
  color: #999;
}
</style>